package dl.bukkit.myststus.api.listener;

import dl.bukkit.myststus.MyStatus;
import dl.bukkit.myststus.api.event.PlayerHealthEvent;
import dl.bukkit.myststus.configuration.imp.Config;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.player.PlayerRespawnEvent;

public class HealthListener implements IListener {

    private final MyStatus plugin;

    public HealthListener(MyStatus plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    void on(EntityDamageEvent event) {
        if (event.getEntity() instanceof Player) {
            this.callEvent((Player) event.getEntity());
        }
    }

    @EventHandler
    void on(EntityRegainHealthEvent event) {
        if (event.getEntity() instanceof Player) {
            this.callEvent((Player) event.getEntity());
        }
    }

    @EventHandler
    void on(EntityDamageByEntityEvent event) {
        if (event.getEntity() instanceof Player) {
            this.callEvent((Player) event.getEntity());
        }
    }

    @EventHandler
    void on(EntityDamageByBlockEvent event) {
        if (event.getEntity() instanceof Player) {
            this.callEvent((Player) event.getEntity());
        }
    }

    @EventHandler
    void on(PlayerRespawnEvent event) {
        this.callEvent(event.getPlayer());
    }

    private void callEvent(Player player) {
        Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> Bukkit.getPluginManager().callEvent(new PlayerHealthEvent(player)));
    }

    @Override
    public boolean isRegister() {
        return Config.getInst().getBoolean("Trigger.Health");
    }
}
